#
# perc2 - calculation of the fractal dimension of correlated
# bond percolation cluster hulls
#
# Copyright (C) 2009, 2010 Indrek Mandre <indrek(at)mare.ee>
# http://www.mare.ee/indrek/perc2/, http://code.google.com/p/perc2/
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# 

import math, sys

STUD95 = 1.96
STUD = STUD95

f = open (sys.argv[1], 'r')
data = []
for line in f:
  if line[0].isdigit():
    vals = line.strip().split(',')
    data.append (map(lambda x: float(x), vals))

def calc(L2,L2STD,L1,L1STD,A2,A1):
  if L1 == 0 or L2 == 0:
    return (0, 0)
  div = math.log (A2 / A1)
  ec1 = -L1STD/(div * L1)
  ec2 = L2STD/(div * L2)
  return (math.log (L2 / L1) / div, math.sqrt(ec1 * ec1 + ec2 * ec2) * STUD)

A = []
M = 0
N = []
vals = []
for l in data:
  (a,m,n) = l[0:3]
  m = int(m)
  M = m
  A.append (a)
  N.append (n)
  row = []
  covm = []
  idx = 0
  sqavg = []
  for j in range(0,m):
    row.append (l[3 + j] / n)
    sqavg.append (range(0,m))

  for j in range(0,m):
    for k in range (j,m):
      sqavg[j][k] = l[3 + m + idx] / n
      sqavg[k][j] = sqavg[j][k]
      idx = idx + 1
  for j in range(0,m):
    row.append (math.sqrt((sqavg[j][j] - row[j]*row[j])/n))
  vals.append (row)

for i in range(1,len(vals)):
  res = []
  res.append (math.sqrt(A[i]*A[i-1]))
  for j in range(0,M):
    (d, err) = calc(vals[i][j], vals[i][M + j], vals[i-1][j], vals[i-1][M + j], A[i], A[i-1])
    res.append (d)
    res.append (err)
  print ' '.join(map(lambda x: "%.8f" % (x, ), res))

